package com.kuang.controller;

import com.kuang.pojo.SystemDept;
import com.kuang.pojo.SystemEmployee;
import com.kuang.service.DepartmentService;
import com.kuang.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class EmployeeController {

//    @Resource
//    EmployeeDao employeeDao;
//    @Resource
//    DepartmentDao departmentDao;
//    @Resource
//    EmployeeMapper employeeMapper;
//    @Resource
//    DepartmentMapper departmentMapper;
    @Resource
    DepartmentService departmentService;
    @Autowired
    EmployeeService employeeService;

    //已更新
    @RequestMapping("/emps")
    public String list(Model model){
        //employeeDao.getAll().var
//        Collection<Employee> employees = employeeDao.getAll();
//        model.addAttribute("emps",employees);
//        List<Employee> employees = employeeMapper.getEmployees();
        List<SystemEmployee> employees = employeeService.getEmployees();
        model.addAttribute("emps",employees);
        return "emp/list";
    }

    //已更新//已更新
    @GetMapping("/emp")
    public String toAddPage(Model model){
        //查出所有部门的讯息
//        Collection<Department> departments = departmentDao.getDepartments();
//        model.addAttribute("departments",departments);
        List<SystemDept> departments = departmentService.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    //已更新
    @PostMapping("/emp")
    public String addEmp(SystemEmployee employee){
        System.out.println(employee+"employee");
        //添加操作   forward
//        employeeDao.save(employee);  //调用底层业务逻辑的方法保存员工信息
//        employeeMapper.save(employee);
        employeeService.save(employee);
        return "redirect:/emps";
    }

    //已更新
    //去员工的修改页面
    @GetMapping("/emp/{emp_code}")
    public String toUpdateEmp(@PathVariable("emp_code") String emp_code , Model model){
        //查出原来的数据
//        Employee employee = employeeDao.getEmployeeByid(id);
//        model.addAttribute("emp",employee);
//        Employee employee = employeeMapper.get(id);
        SystemEmployee employee = employeeService.get(emp_code);
        model.addAttribute("emp",employee);
        //查出所有部门的讯息
//        Collection<Department> departments = departmentDao.getDepartments();
//        model.addAttribute("departments",departments);
        List<SystemDept> departments = departmentService.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/update";
    }

    //已经更新
    @PostMapping("/updateEmp")
    public String updateEmp(SystemEmployee employee){
//        employeeDao.save(employee);
//        employeeMapper.update(employee);
        employeeService.update(employee);
        return "redirect:/emps";
    }

    //已更新
    //删除员工
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") String id){
//        employeeDao.delete(id);
//        employeeMapper.delete(id);
        employeeService.delete(id);
        return "redirect:/emps";
    }

    @RequestMapping("/user/logout")
    public String logout(HttpSession session){
        session.invalidate();

        return  "redirect:/index.html";
    }

}
